//
//  SpeedView.swift
//  Trans
//
//  Created by zcj on 2024/12/6.
//  description: 控制速度的页面
//  推荐实现一个view来实现每列的数据实现

import SwiftUI
import Network

struct SpeedView: View {
    @ObservedObject var  conDic = kzListener.conDic
    
    var body: some View {
        let arr = Array(conDic.keys())
        //        let arr = Array(netDic.dic.keys)
        GeometryReader{ geometry in
            VStack{
                HStack{
                    Spacer()
                    Text(NSLocalizedString("speedCtrl", comment: "default")).titleStyle()
                    Spacer()
                }
                ScrollView{
                    ForEach(arr,id: \.self){ conId in
                        if let cm = conDic[conId]{
                            let bindCm = Binding<KzConMsg>(
                                get: {conDic[conId]!},
                                set: {conDic[conId] = $0 }
                            )
                            HStack{
                                Text(cm.hostName).frame(width: geometry.size.width * 0.2 )
                                Divider()
                                    .background(kcolors.lineColor)
                                    .padding(.vertical,0)
                                let fileList = Array(cm.fileDic.keys)
                                ForEach(fileList,id: \.self){ fId in
                                    VStack{
                                        Text((cm.fileDic[fId]?.fileName)!).frame(width: geometry.size.width * 0.6 )
                                    }
                                }
                                Divider()
                                    .background(kcolors.lineColor)
                                    .padding(.vertical,0)
                                //设置速度的输入框和确定按钮
                                Text("调整速度上限:")
                                TextField("3M",text:bindCm.speed)
                                Button {
                                    sendSpeedReq(connection: cm.con, speed: cm.speed)
                                } label: {
                                    Text("确定")
                                }
                                .frame(height: 20.0 * CGFloat(cm.fileDic.count > 0 ? cm.fileDic.count:1))
                                Divider()
                                    .background(kcolors.lineColor)
                                    .padding(.horizontal,0)
                            }
                        }
                    }
                }
            }
            
        }
    }
}

func sendSpeedReq(connection:NWConnection,speed:String){
    //转换为FSIZE
    print("speed: \(speed)")
    let spdSize = str2SizeFormat(str: speed)
    //发送
    let spdMsg = SpeedMessage(speed: spdSize)
    
    if   let jsonData =  SpeedMessage.enCodeToJson(obj: spdMsg){
        let pkgData = PackageHead.packageData(msgData: jsonData, type: SPEED_REQ)
        connection.send(content: pkgData, completion: .contentProcessed({ error in
            if let error = error{
                error_log("error: sendSpeedReq \(error)")
            }else{
                print("sendSpeedReq success ")
            }
        }))
    }
    
}



//#Preview {
//    SpeedView()
//}
